﻿using BBICMS.Model.ViewModel.Back;
using BBICMS.Repository;
using BBICMS.WebUI.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;
using BBICMS.Model;
using BBICMS.ViewModel;
using BBICMS.Resources;
using BBICMS.Application.ViewModels;

namespace BBICMS.WebUI.Areas.Admin.Controllers
{
     [SIGAuth]
    
    public class TeamController : BaseController
    {
         
        // GET: /Admin/Team/
        public ActionResult Index(int? page)
        {
            int PageSize = BBICMS.Infrastructure.Helpers.Settings.PageSize;

            TeamListVM TeamList = new TeamListVM
            {
                Teams = _unit.Teams.GetAll().OrderByDescending(s => s.Importance).ThenByDescending(t=>t.AddedDate),
                PageSize = BBICMS.Infrastructure.Helpers.Settings.PageSize,
                PageNumber = page ?? 1
            };

            //var pageNumber = page ?? 1;
            //var onePageOfProducts = TeamList.Teams.ToPagedList(pageNumber, PageSize);
            //ViewBag.OnePageOfProducts = onePageOfProducts;

            return View(TeamList);
        }

        
         [HttpGet]
         public ActionResult Add()
         {
             Team vTeam = new Team();
             vTeam.Active = true;
             vTeam.Importance = 0;

             return View("Add", vTeam);
         }


        
         [HttpPost]
         [ValidateInput(false)]   
         public JsonResult Add(Team team)
         {                         

             if (ModelState.IsValid)
             {
                 _unit.Teams.Add(team);
                 _unit.SaveChanges();

                AR.SetSuccess(string.Format(Messages.AlertCreateSuccess, EntityNames.Team));
                return Json(AR, JsonRequestBehavior.DenyGet); 
             }

            AR.Setfailure(string.Format(Messages.AlertCreateFailure, EntityNames.Team));
            return Json(AR, JsonRequestBehavior.DenyGet);

        }


        
         public ActionResult Edit(int Id)
         {
             var vTeam = _unit.Teams.GetById(Id);
             if (vTeam != null)
             {
                 return View(vTeam);
             }
             return View("NotFound");
         }

         [HttpPost]
         [ValidateInput(false)]
        
         public JsonResult Edit(Team team)
         {                    
             //site.UpdatedBy = User.Identity.Name;
             //site.UpdatedDate = DateTime.Now;   

             if (ModelState.IsValid)
             {
                 _unit.Teams.Update(team);
                 _unit.SaveChanges();

                AR.SetSuccess(string.Format(Messages.AlertUpdateSuccess, EntityNames.Team));
                return Json(AR, JsonRequestBehavior.DenyGet);
            }

            AR.Setfailure(string.Format(Messages.AlertUpdateFailure, EntityNames.Team));
            return Json(AR, JsonRequestBehavior.DenyGet);

        }


    }
}
